package com.jnu.mydiary.ui.users;


import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.jnu.mydiary.R;
import com.jnu.mydiary.ui.shouye1.DataBank;

import java.io.Serializable;

public class EditUserActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_user);
        DataBank mDataBank=new DataBank(this);
        User user=mDataBank.loadUser();
        EditText editTextName=findViewById(R.id.editText_name);
        String name1,talk1,mail1,phone1;


        name1=user.getUserName();
        if(null!=name1){
            editTextName.setText(name1);
        }
        EditText editTextTalk=findViewById(R.id.editText_talk);
        talk1=user.getMark();
        if(null!=talk1){
            editTextTalk.setText(talk1);
        }
        EditText editTextMail=findViewById(R.id.editText_mail);
        mail1=user.getMail();
        if(null!=mail1){
            editTextMail.setText(mail1);
        }
        EditText editTextPhone=findViewById(R.id.editText_phone);
        phone1=user.getPhone();
        if(null!=phone1){
            editTextPhone.setText(phone1);
        }



        Button buttonOk=this.findViewById(R.id.button_ok);
        buttonOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();

                String name=editTextName.getText().toString();
                String talk=editTextTalk.getText().toString();
                String mail=editTextMail.getText().toString();
                String phone=editTextPhone.getText().toString();

                User mUser=new User(name,talk,phone,mail);

                mDataBank.saveUser(mUser);

                intent.putExtra("name",name);
                intent.putExtra("talk",talk);
                intent.putExtra("mail",mail);
                intent.putExtra("phone",phone);
                intent.putExtra("type","user");
                intent.putExtra("now_day",0);
                setResult(1,intent);//requestCode=1
                EditUserActivity.this.finish();
            }
        });
        Button buttonCancel=this.findViewById(R.id.button_cancel);
        buttonCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditUserActivity.this.finish();
            }
        });
    }
}